package weaver.interfaces.zhlx.duss.action.financemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 冲暂估单流程调用SAP会计凭证接口（FI092）
 * @ModifyLog:
 **/
public class WeaCzgdlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaCzgdlcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            String currentDate = DateUtil.getCurrentDate();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String gysdm = Util.null2String(rs.getString("gysdm"));
            String sjkm = Util.null2String(rs.getString("sjkm"));
            String sjkmmc = Util.null2String(rs.getString("sjkmmc"));
            String sehj = Util.null2String(rs.getString("sehj"));
            String zftj = Util.null2String(rs.getString("zftj"));
            String jxsezckm = Util.null2String(rs.getString("jxsezckm"));
            String jxsezckmmc = Util.null2String(rs.getString("jxsezckmmc"));
            String jxzcje = Util.null2String(rs.getString("jxzcje"));
            String bccxhj = Util.null2String(rs.getString("bccxhj"));
            String bccxbbhj = Util.null2String(rs.getString("bccxbbhj"));
            String rzsj = Util.null2String(rs.getString("rzsj"));
            JSONObject dataJson = new JSONObject();
            JSONObject headerJson = new JSONObject();
            headerJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
            headerJson.put("BLART", Util.null2String(rs.getString("pzlx")));
            headerJson.put("WAERS", Util.null2String(rs.getString("bzwb")));
            headerJson.put("KURSF", "");
            headerJson.put("BLDAT", rzsj.replace("-", ""));
            headerJson.put("BUDAT", rzsj.replace("-", ""));
            headerJson.put("XBLNR", Util.null2String(rs.getString("lcbh")));
            headerJson.put("XBLNR_ALT", Util.null2String(rs.getString("htlcbh")));
            headerJson.put("BKTXT", "");
            String fphm = Util.null2String(rs.getString("fphm"));
            headerJson.put("ZJINSHUIHAOMA", fphm.replace(",", "、"));
            headerJson.put("BELNR", "");
            headerJson.put("USNAM", Util.null2String(rs.getString("jbrwb")));
            headerJson.put("USNAM2", Util.null2String(rs.getString("gh")));
            headerJson.put("ZDANJULEIXING", "冲销暂估");
            dataJson.put("HEADER", headerJson);
            JSONArray itemList = new JSONArray();
            String dfkm = "";
            String dfkmmc = "";
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
            int count = 1;
            while (rs.next()) {
                if (count == 1) {
                    dfkm = Util.null2String(rs.getString("dfkm"));
                    dfkmmc = Util.null2String(rs.getString("dfkmmc"));
                }
                String bccx = Util.null2String(rs.getString("bccx"));
                String bccxbb = Util.null2String(rs.getString("bccxbb"));
                String bcrzjews = Util.null2String(rs.getString("bcrzjews"));
                String bcrzjewsbb = Util.null2String(rs.getString("bcrzjewsbb"));
                double bccxDouble = Util.getDoubleValue(bccx);
                double bcrzjewsDouble = Util.getDoubleValue(bcrzjews);
                String xh = Util.null2String(rs.getString("xh"));
                String fykm = Util.null2String(rs.getString("fykm"));
                String fykmmc = Util.null2String(rs.getString("fykmmc"));
                String cbzxwb = Util.null2String(rs.getString("cbzxwb"));
                String nbddwb = Util.null2String(rs.getString("nbddwb"));
                String wbswb = Util.null2String(rs.getString("wbswb"));
                String zgxhwb = Util.null2String(rs.getString("zgxhwb"));
                if (bccxDouble > 0) {
                    JSONObject itemJson = new JSONObject();
                    itemJson.put("BUZEI", count);
                    itemJson.put("HKONT", fykm);
                    itemJson.put("ZFEIYONGLEIXING", fykmmc);
                    itemJson.put("KOSTL", cbzxwb);
                    itemJson.put("AUFNR", nbddwb);
                    itemJson.put("PROJK", wbswb);
                    itemJson.put("WRBTR", "-" + bccx);
                    itemJson.put("DMBTR", "-" + bccxbb);
                    itemJson.put("ZFBDT", currentDate.replace("-", ""));
                    itemJson.put("ZUONR", zgxhwb);
                    itemList.add(itemJson);
                    count++;
                }
                if (bcrzjewsDouble > 0) {
                    JSONObject itemJson = new JSONObject();
                    itemJson.put("BUZEI", count);
                    itemJson.put("HKONT", fykm);
                    itemJson.put("ZFEIYONGLEIXING", fykmmc);
                    itemJson.put("KOSTL", cbzxwb);
                    itemJson.put("AUFNR", nbddwb);
                    itemJson.put("PROJK", wbswb);
                    itemJson.put("WRBTR", bcrzjews);
                    itemJson.put("DMBTR", bcrzjewsbb);
                    itemJson.put("ZFBDT", currentDate.replace("-", ""));
                    itemJson.put("ZUONR", xh);
                    itemList.add(itemJson);
                    count++;
                }
            }

            String sql23 = "select dfkm,dfkmmc,sum(je) as je,sum(jebb) as jebb from ( " +
                    "select dfkm,dfkmmc,sum(hsje) as je,sum(hsjebb) as jebb from "+tablename+"_dt3 where mainid=? group by dfkm,dfkmmc " +
                    "union all " +
                    "select rzdfkm,rzdfkmmc,sum(bcrzjehs) as je,sum(bcrzjehsbb) as jebb from "+tablename+"_dt2 where mainid=? group by rzdfkm,rzdfkmmc " +
                    ") a group by dfkm,dfkmmc";
            rs.executeQuery(sql23, mainid,mainid);
            while (rs.next()) {
                JSONObject itemJson = new JSONObject();
                itemJson.put("BUZEI", count);
                itemJson.put("HKONT", Util.null2String(rs.getString("dfkm")));
                itemJson.put("ZFEIYONGLEIXING", Util.null2String(rs.getString("dfkmmc")));
                itemJson.put("LIFNR", gysdm);
                itemJson.put("WRBTR", Util.null2String(rs.getString("je")));
                itemJson.put("DMBTR", Util.null2String(rs.getString("jebb")));
                itemJson.put("ZFBDT", currentDate.replace("-", ""));
                itemJson.put("ZTERM", zftj);
                itemList.add(itemJson);
                count++;
            }

            rs.executeQuery("select * from " + tablename + "_dt3 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject itemJson = new JSONObject();
                itemJson.put("BUZEI", count);
                itemJson.put("HKONT", Util.null2String(rs.getString("fykm")));
                itemJson.put("ZFEIYONGLEIXING", Util.null2String(rs.getString("fykmmc")));
                itemJson.put("KOSTL", Util.null2String(rs.getString("cbzxwb")));
                itemJson.put("AUFNR", Util.null2String(rs.getString("nbddwb")));
                itemJson.put("PROJK", Util.null2String(rs.getString("wbswb")));
                itemJson.put("WRBTR", Util.null2String(rs.getString("wsje")));
                itemJson.put("DMBTR", Util.null2String(rs.getString("wsjebb")));
                itemJson.put("ZFBDT", currentDate.replace("-", ""));
                itemJson.put("ZUONR", Util.null2String(rs.getString("xh")));
                itemList.add(itemJson);
                count++;
            }

            JSONObject itemJsonMain1 = new JSONObject();
            itemJsonMain1.put("BUZEI", count);
            itemJsonMain1.put("HKONT", dfkm);
            itemJsonMain1.put("ZFEIYONGLEIXING", dfkmmc);
            itemJsonMain1.put("LIFNR", gysdm);
            itemJsonMain1.put("WRBTR", "-"+bccxhj);
            itemJsonMain1.put("DMBTR", "-"+bccxbbhj);
            itemJsonMain1.put("ZFBDT", currentDate.replace("-", ""));
            itemJsonMain1.put("ZTERM", zftj);
            itemList.add(itemJsonMain1);
            count++;
            JSONObject itemJsonMain3 = new JSONObject();
            itemJsonMain3.put("BUZEI", count);
            itemJsonMain3.put("HKONT", sjkm);
            itemJsonMain3.put("ZFEIYONGLEIXING", sjkmmc);
            itemJsonMain3.put("WRBTR", sehj);
            itemJsonMain3.put("DMBTR", sehj);
            itemJsonMain3.put("ZFBDT", currentDate.replace("-", ""));
            itemList.add(itemJsonMain3);
            count++;
            JSONObject itemJsonMain4 = new JSONObject();
            itemJsonMain4.put("BUZEI", count);
            itemJsonMain4.put("HKONT", jxsezckm);
            itemJsonMain4.put("ZFEIYONGLEIXING", jxsezckmmc);
            itemJsonMain4.put("WRBTR", jxzcje);
            itemJsonMain4.put("DMBTR", jxzcje);
            itemJsonMain4.put("ZFBDT", currentDate.replace("-", ""));
            itemList.add(itemJsonMain4);
            count++;
            dataJson.put("ITEM", itemList);
            writeLog(">>>>>WeaCzgdlcAction>>>>>dataJson=" + dataJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaCzgdlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            JSONObject resultJson = sapUtil.handleOKHttpRequest("FI092", dataJson, dataMap);
            writeLog(">>>>>WeaCzgdlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String dataStr = outputJson.getString("DATA");
                JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                if ("S".equalsIgnoreCase(msgType)) {
                    String belnrs = "";
                    String bukrs = "";
                    String gjahr = "";
                    for (int i = 0; i < returnList.size(); i++) {
                            String belnr = returnList.getJSONObject(i).getString("MSG_BELNR");
                            belnrs += "".equals(belnrs) ? belnr : "," + belnr;
                            bukrs = returnList.getJSONObject(i).getString("MSG_BUKRS");
                            gjahr = returnList.getJSONObject(i).getString("MSG_GJAHR");
                    }
                    String sql = "update " + tablename + " set sappzh=?,sapgsdm=?,hjnd=? where requestid=?";
                    rs.executeUpdate(sql, belnrs, bukrs, gjahr, requestid);
                } else {
                    for (int j = 0; j < returnList.size(); j++) {
                        String msgType1 = returnList.getJSONObject(j).getString("MSG_TYPE");
                        if("E".equalsIgnoreCase(msgType1)){
                            message += returnList.getJSONObject(j).getString("MSG_TEXT")+";";
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaCzgdlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}